﻿using SIAKAD.Helper;
using SIAKADCORE.Controllers;
using SIAKADCORE.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SIAKAD
{
    public partial class ViewRekomendasi : System.Web.UI.Page
    {
        private DataSet dsRekomendasi;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsCallback)
            {
                this.InitTabelRekomendasi();
            }
        }

        #region Helpers
        private void InitTabelRekomendasi()
        {
            bool isSuccess = RekomendasiController.GetTabel(out this.dsRekomendasi);
            if (isSuccess)
            {
                this.gvRekomendasi.DataSource = this.dsRekomendasi.Tables[0];
                this.gvRekomendasi.DataBind();
            }
        }

        protected void SetHiddenField(int index, ref HiddenField hiddenField)
        {
            string code = gvRekomendasi.DataKeys[index].Value.ToString();
            hiddenField.Value = code;
        }

        protected void DeleteCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfDeleteID);

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDeleteModalScript", RekomendasiScript.ShowDelete(), false);
        }

        protected void EditCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfUpdateID);
            this.txtEditRekomendasi.Text = gvRekomendasi.Rows[index].Cells[1].Text;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowEditModalScript", RekomendasiScript.ShowEdit(), false);
        }
        #endregion

        #region Events
        protected void gvRekomendasi_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("deleteRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.DeleteCommand(index);
            }
            else if (e.CommandName.Equals("updateRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.EditCommand(index);
            }
        }

        protected void gvRekomendasi_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvRekomendasi.PageIndex = e.NewPageIndex;
            this.InitTabelRekomendasi();

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "PagingScript", SiakadScript.PagerStyle(), false);
        }

        protected void btAdd_Click(object sender, EventArgs e)
        {
            this.txtAddRekomendasi.Text = string.Empty;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowAddModalScript", RekomendasiScript.ShowAdd(), false);
        }

        protected void btAddRekomendasi_Click(object sender, EventArgs e)
        {
            var rekomendasi = new Rekomendasi
            {
                IdRekomendasi = RekomendasiController.GetID(),
                IsiRekomendasi = txtAddRekomendasi.Text
            };

            bool isSuccess = RekomendasiController.Tambah(rekomendasi);
            if (isSuccess)
            {
                this.InitTabelRekomendasi();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideAddModalScript", RekomendasiScript.SuccessAdd(), false);
            }
        }

        protected void btEditRekomendasi_Click(object sender, EventArgs e)
        {
            var rekomendasi = new Rekomendasi
            {
                IdRekomendasi = this.hfUpdateID.Value,
                IsiRekomendasi = txtEditRekomendasi.Text
            };

            bool isSuccess = RekomendasiController.Ubah(this.hfUpdateID.Value, rekomendasi);
            if (isSuccess)
            {
                this.InitTabelRekomendasi();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideEditModalScript", RekomendasiScript.SuccessEdit(), false);
            }
        }

        protected void btDeleteRekomendasi_Click(object sender, EventArgs e)
        {
            bool isSuccess = RekomendasiController.Hapus(hfDeleteID.Value);
            if (isSuccess)
            {
                this.InitTabelRekomendasi();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideDeleteModalScript", RekomendasiScript.SuccessDelete(), false);
            }
        }     
        #endregion
    }
}